package com.lytxmd.boxtest.ui.main;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.lytxmd.boxtest.R;
import com.lytxmd.boxtest.base.MvpBaseActivity;
import com.lytxmd.boxtest.bean.DeviceInfo;
import com.lytxmd.boxtest.ui.bindwifi.BindWifiActivity;
import com.lytxmd.boxtest.ui.debug.DebugBoxActivity;
import com.lytxmd.boxtest.utils.LogUtils;
import com.lytxmd.boxtest.utils.RouterUtils;
import com.lytxmd.boxtest.utils.ToastUtils;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import butterknife.BindView;

public class MainActivity extends MvpBaseActivity<MainContract.MainPresenter> implements MainContract.MainView {
    private static final int QR_REQUEST_CODE = 100;

    @BindView(R.id.box_id)
    EditText boxId;
    @BindView(R.id.debug_btn)
    Button debugBtn;
    @BindView(R.id.scan_qrcode)
    Button scanQrcode;
    private String deviceNumber;

    @Override
    protected void initParams(Bundle params) {
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        initToolBarNoBack("盒子调试");
        debugBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deviceNumber = boxId.getText().toString().replace(" ","").trim();
                if(TextUtils.isEmpty(deviceNumber)||deviceNumber.length()!=12){
                    ToastUtils.show(mContext,"请输入正确的设备编号");
                    boxId.setFocusable(true);
                    return;
                }
                LogUtils.i(TAG,"请求设备信息");
                LogUtils.i(TAG,deviceNumber);
                mPresenter.getDeviceInfo(deviceNumber);
            }
        });

        scanQrcode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RouterUtils.gotoNextForResult(MainActivity.this, CaptureActivity.class, QR_REQUEST_CODE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == QR_REQUEST_CODE) {
            //处理扫描结果（在界面上显示）
            if (null != data) {
                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                    deviceNumber = bundle.getString(CodeUtils.RESULT_STRING).replace(" ","").trim();
//                    ToastUtils.show(mContext,"解析结果:" + result);

                    if(TextUtils.isEmpty(deviceNumber)||deviceNumber.length()!=12){
                        ToastUtils.show(mContext,"请扫描设备上的二维码");
                        return;
                    }
                    mPresenter.getDeviceInfo(deviceNumber.trim());
                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                    ToastUtils.show(mContext,"解析二维码失败");
                }
            }
        }
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_main;
    }

    @Override
    protected MainContract.MainPresenter createPresenter() {
        return new MainPresenterImpl(this);
    }


    /*impl MainView*/
    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void getDeviceInfoSucc(DeviceInfo deviceInfo) {
        LogUtils.i(TAG,"--------------success---------------");
        Bundle bundle = new Bundle();
        bundle.putSerializable("debugDeviceInfo",deviceInfo);
        RouterUtils.gotoNext(this, DebugBoxActivity.class,bundle);
    }

    @Override
    public void getDeviceInfoFail(String msg) {
        LogUtils.i(TAG,msg+"--------------fail---------------");
        AlertDialog dialog =  new AlertDialog.Builder(mContext)
                .setTitle("配置网络")
                .setMessage("请配置硬件设备网络,如已配置网络,请忽略配置过程.")
                .setNegativeButton("已配置", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ToastUtils.show(mContext,"请检查盒子当前网络是否可用");
                    }
                })
                .setPositiveButton("配置网络", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        RouterUtils.gotoNext(mContext, BindWifiActivity.class);
                    }
                }).create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
    }
}
